/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.acq.web.breedtype;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.acq.entity.breedtype.AcqBreedType;
import com.thinkgem.jeesite.modules.acq.service.breedtype.AcqBreedTypeService;

/**
 * 养殖品类表Controller
 * @author lj
 * @version 2018-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/acq/breedtype/acqBreedType")
public class AcqBreedTypeController extends BaseController {

	@Autowired
	private AcqBreedTypeService acqBreedTypeService;
	
	@ModelAttribute
	public AcqBreedType get(@RequestParam(required=false) String id) {
		AcqBreedType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = acqBreedTypeService.get(id);
		}
		if (entity == null){
			entity = new AcqBreedType();
		}
		return entity;
	}
	
	@RequiresPermissions("acq:breedtype:acqBreedType:view")
	@RequestMapping(value = {"list", ""})
	public String list(AcqBreedType acqBreedType, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<AcqBreedType> list = acqBreedTypeService.findList(acqBreedType); 
		model.addAttribute("list", list);
		return "modules/acq/breedtype/acqBreedTypeList";
	}

	@RequiresPermissions("acq:breedtype:acqBreedType:view")
	@RequestMapping(value = "form")
	public String form(AcqBreedType acqBreedType, Model model) {
		if (acqBreedType.getParent()!=null && StringUtils.isNotBlank(acqBreedType.getParent().getId())){
			acqBreedType.setParent(acqBreedTypeService.get(acqBreedType.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(acqBreedType.getId())){
				AcqBreedType acqBreedTypeChild = new AcqBreedType();
				acqBreedTypeChild.setParent(new AcqBreedType(acqBreedType.getParent().getId()));
				List<AcqBreedType> list = acqBreedTypeService.findList(acqBreedType); 
				if (list.size() > 0){
					acqBreedType.setSort(list.get(list.size()-1).getSort());
					acqBreedType.setCode(list.get(list.size()-1).getCode());
					if (acqBreedType.getSort() != null){
						acqBreedType.setSort(acqBreedType.getSort() + 1);
					}
					if (acqBreedType.getCode() != null){
						acqBreedType.setCode(String.valueOf((int)((Math.random()*9+1)*100000)));
					}
				}
			}
		}
		if (acqBreedType.getSort() == null){
			acqBreedType.setSort(30);
		}
		if (acqBreedType.getCode() == null){
			acqBreedType.setCode(String.valueOf((int)((Math.random()*9+1)*100000)));
		}
		model.addAttribute("acqBreedType", acqBreedType);
		return "modules/acq/breedtype/acqBreedTypeForm";
	}

	@RequiresPermissions("acq:breedtype:acqBreedType:edit")
	@RequestMapping(value = "save")
	public String save(AcqBreedType acqBreedType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, acqBreedType)){
			return form(acqBreedType, model);
		}
		acqBreedTypeService.save(acqBreedType);
		addMessage(redirectAttributes, "保存养殖品类成功");
		return "redirect:"+Global.getAdminPath()+"/acq/breedtype/acqBreedType/?repage";
	}
	
	@RequiresPermissions("acq:breedtype:acqBreedType:edit")
	@RequestMapping(value = "delete")
	public String delete(AcqBreedType acqBreedType, RedirectAttributes redirectAttributes) {
		acqBreedTypeService.delete(acqBreedType);
		addMessage(redirectAttributes, "删除养殖品类成功");
		return "redirect:"+Global.getAdminPath()+"/acq/breedtype/acqBreedType/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<AcqBreedType> list = acqBreedTypeService.findList(new AcqBreedType());
		for (int i=0; i<list.size(); i++){
			AcqBreedType e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}